<?php

/**
 * This is the model class for table "theme".
 *
 * The followings are the available columns in table 'theme':
 * @property string $id
 * @property string $parent_id
 * @property string $prev
 * @property string $next
 * @property string $title
 * @property string $body
 * @property string $meta_keywords
 * @property string $meta_description
 */
class Theme extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Theme the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'theme';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('parent_id, prev, next', 'length', 'max'=>11),
			array('title, meta_keywords, meta_description', 'length', 'max'=>255),
			array('body', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, parent_id, prev, next, title, body, meta_keywords, meta_description', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    'getparent' => array(self::BELONGS_TO, 'Theme', 'parent_id'),
                    'childs' => array(self::HAS_MANY, 'Theme', 'parent_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'parent_id' => 'Parent',
			'prev' => 'Prev',
			'next' => 'Next',
			'title' => 'Title',
			'body' => 'Body',
			'meta_keywords' => 'Meta Keywords',
			'meta_description' => 'Meta Description',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('parent_id',$this->parent_id,true);
		$criteria->compare('prev',$this->prev,true);
		$criteria->compare('next',$this->next,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('body',$this->body,true);
		$criteria->compare('sort',$this->sort,true);
		$criteria->compare('meta_keywords',$this->meta_keywords,true);
		$criteria->compare('meta_description',$this->meta_description,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        /**
         * Function return parent themes for breadcrums by theme id
         * @param integer $theme_id
         * @return mixed AR_collection 
         */
        public static function getBreadCrumsByThemeId($theme_id){
            
            $return_array = array(); $exitwhile = false;
            $criteria=new CDbCriteria;
            $criteria->select='id,parent_id,title';
            $themes = self::model()->findAll();
            $themes_ar = array();
            
            if(count($themes)){
                $i = 0; $themes_ar = array();
                foreach($themes as $theme){
                    $themes_ar[$i] = $theme->attributes;
                    $i++;
                }
            }
            
            if(!empty($themes_ar)) 
                $return_array = self::_getParentForBread($themes_ar, $theme_id);
            $return_array = array_reverse($return_array);
            $last = count($return_array); $i = 1;
            foreach($return_array as $key => $value){
                if($i == $last){
                    $return_array[$key] = '' ;
                }
                $i++;
            }
            return $return_array;
        }
        
        private function _getParentForBread($array, $id){
            $return_array = array();
            foreach($array as $arr){
                if($arr['id'] == $id){
                    $return_array[$arr['title']] = array('theme/view', 'id'=>(int)$arr['id']);
                    $parent_id = $arr['parent_id'];
                }
            }
            while ($parent_id != 0){
                foreach($array as $arr){
                    if($arr['id'] == $parent_id) {
                        $return_array[$arr['title']] = array('theme/view','id'=>(int)$parent_id);
                        $parent_id = $arr['parent_id'];
                    }
                }
            }
            return $return_array;
        }
        
        protected function beforeValidate($on)
        {
            $parser=new MarkdownParserHighslide;
        }
}